﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Web;
using SHJunketingBLL;

using SHJunketingModel;

public partial class Admin_UserManagement_userEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Session["userid"] != null)
            {
                string userid = Session["userid"].ToString();
                ViewState["userid"] = userid;
                string sql = "select * from users where userid = " + userid;
                DataTable table = DBHelper.GetDataSet(sql);
                if (table.Rows.Count > 0)
                {
                    this.txtName.Text = table.Rows[0]["userlogin"].ToString();
                    txtUserName.Text = table.Rows[0]["UserName"].ToString();
                    rdoSex.SelectedValue = table.Rows[0]["UserSex"].ToString();
                    txtDepartment.Text = table.Rows[0]["Department"].ToString();
                    txtCostCenters.Text = table.Rows[0]["CostCenters"].ToString();
                    txtUserIDCard.Text = table.Rows[0]["UserIDCard"].ToString();
                    txtUserAge.Text = table.Rows[0]["UserAge"].ToString();
                    rdoMarriage.SelectedValue = table.Rows[0]["Marriage"].ToString();
                    txtPhoneNumber.Text = table.Rows[0]["HandsetNumber"].ToString();
                    txtTelephone.Text = table.Rows[0]["TelephoneNumber"].ToString();
                    txtAddress.Text = table.Rows[0]["UserAddress"].ToString();
                    txtUserEmai.Text = table.Rows[0]["UserEmail"].ToString();
                    txtArea.Text = table.Rows[0]["Area"].ToString();
                    rdoLang.SelectedValue = table.Rows[0]["language"].ToString();
                    txtpwd.Text = "●●●●●●";
                    txtpwd1.Text = "●●●●●●";
                    ddlYuyuefenlei.SelectedValue = table.Rows[0]["yuyuefenleiid"].ToString();
                }

               C_YuyueType YuyueType=  C_YuyueTypeManage.GetYuyueTypeByID(int.Parse(ddlYuyuefenlei.SelectedValue));

               this.lblTime.Text = YuyueType.Starttime + "/" + YuyueType.Endtime;
            }
        }
    }

    protected void btnEbit_Click(object sender, EventArgs e)
    {
        string sql = "";
        if (this.CheckBox1.Checked == true)
        {
            if (this.txtpwd.Text == "")
            {
                Web.CommFun.Show_Msg("密码不能为空！");
                return;
            }
            else if (txtpwd1.Text == "")
            {
                Web.CommFun.Show_Msg("确认密码不能为空！");
                return;
            }
            else if (!txtpwd.Text.Equals(txtpwd1.Text))
            {
                Web.CommFun.Show_Msg("两次密码必须一致！");
                return;
            }
            else
            {
                 sql =
                 "UPDATE users " +
                 "SET " +
                     "UserLogin = '" + txtName.Text.Trim() + "', " +
                     "UserName = '" + txtUserName.Text.Trim() + "'," +
                     "UserSex = '" + rdoSex.SelectedValue + "'," +
                     "Department = '" + txtDepartment.Text.Trim() + "'," +
                     "CostCenters  = '" + txtCostCenters.Text.Trim() + "'," +
                     "UserIDCard = '" + txtUserIDCard.Text.Trim() + "'," +
                     "UserAge = '" + txtUserAge.Text.Trim() + "'," +
                     "Marriage  = '" + rdoMarriage.SelectedValue + "'," +
                     "HandsetNumber = '" + txtPhoneNumber.Text.Trim() + "'," +
                     "TelephoneNumber = '" + txtTelephone.Text.Trim() + "'," +
                     "UserAddress = '" + txtAddress.Text.Trim() + "'," +
                     "UserEmail = '" + txtUserEmai.Text.Trim() + "'," +
                     "Area  = '" + txtArea.Text.Trim() + "'," +
                     "language  = '" + rdoLang.SelectedValue + "'," +
                     "UserPwd  = '" + txtpwd.Text.Trim() + "'" +

                 "WHERE UserID = '" + ViewState["userid"].ToString() + "'";
            }
        }
        else 
        {
             sql =
                 "UPDATE users " +
                 "SET " +
                     "UserLogin = '" + txtName.Text.Trim() + "', " +
                     "UserName = '" + txtUserName.Text.Trim() + "'," +
                     "UserSex = '" + rdoSex.SelectedValue + "'," +
                     "Department = '" + txtDepartment.Text.Trim() + "'," +
                     "CostCenters  = '" + txtCostCenters.Text.Trim() + "'," +
                     "UserIDCard = '" + txtUserIDCard.Text.Trim() + "'," +
                     "UserAge = '" + txtUserAge.Text.Trim() + "'," +
                     "Marriage  = '" + rdoMarriage.SelectedValue + "'," +
                     "HandsetNumber = '" + txtPhoneNumber.Text.Trim() + "'," +
                     "TelephoneNumber = '" + txtTelephone.Text.Trim() + "'," +
                     "UserAddress = '" + txtAddress.Text.Trim() + "'," +
                     "UserEmail = '" + txtUserEmai.Text.Trim() + "'," +
                     "Area  = '" + txtArea.Text.Trim() + "'," +
                     "language  = '" + rdoLang.SelectedValue + "'" +

                 "WHERE UserID = '" + ViewState["userid"].ToString() + "'";
        }

        

       int result = DBHelper.ExecuteCommand(sql);
       if (result > 0)
       {
           Web.CommFun.Show_Msg("修改成功！", "UserContent.aspx");
       }
       else 
       {
           Web.CommFun.Show_Msg("修改失败！", "UserContent.aspx");
       }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
        {


            this.txtpwd1.Enabled = true;
            this.txtpwd.Enabled = true;
            txtpwd.TextMode = TextBoxMode.Password;
            txtpwd1.TextMode = TextBoxMode.Password;
        }
        else 
        {
            this.txtpwd1.Enabled = false;
            this.txtpwd.Enabled = false;
            txtpwd.TextMode = TextBoxMode.SingleLine;
            txtpwd1.TextMode = TextBoxMode.SingleLine;
            txtpwd.Text = "●●●●●●";
            txtpwd1.Text = "●●●●●●";
        }
    }

    protected void ddlYuyuefenlei_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlYuyuefenlei.SelectedValue == "")
        {
            return;
        }
        C_YuyueType type = C_YuyueTypeManage.GetYuyueTypeByID(int.Parse(ddlYuyuefenlei.SelectedValue));
        this.lblTime.Text = type.Starttime + "/" + type.Endtime;
    }
}
